﻿using Prism.Events;
using Prism.Ioc;
using System.Windows;
using Xcj.Mes.WholeLine.ZH.HG.Shell.Events;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.Views
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView : Window
    {
        protected IEventAggregator _eventAggregator { get; private set; }

        public LoginView()
        {
            InitializeComponent();

            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler((o, e) =>
            {
                DragMove();
            });

            // 订阅事件
            _eventAggregator = ((Prism.PrismApplicationBase)Application.Current).Container.Resolve<IEventAggregator>();

            this.Loaded += ((o, e) =>
            {
                try { _eventAggregator?.GetEvent<CloseWindowEvent>()?.Subscribe(CloseWindow); } catch { }
            });

            // 取消订阅
            this.Closed += ((o, e) =>
            {
                try { _eventAggregator?.GetEvent<CloseWindowEvent>()?.Unsubscribe(CloseWindow); } catch { }
            });
        }

        public void OnExit(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void CloseWindow()
        {
            try
            {
                // 如果是 Window
                if (this is Window window)
                    window.Close();
                // 如果是 UserControl，关闭其所属 Window
                else
                    Window.GetWindow(this)?.Close();
            }
            catch { }
        }
    }
}
